﻿using System.Collections.ObjectModel;
using System.Windows;
using Silverlight.Training.Client.Common;
using Silverlight.Training.Client.Common.ServiceReference.Contracts;
using Silverlight.Training.Client.Common.ServiceReference.EventArgs;
using SilverLight.Training.Models;

namespace SilverLight.Training.ViewModels
{
    public class EmployeeTypeDetailsViewModel : NotifyPropertyChangedBase
    {
        private EmployeeTypeDetailsModel model;

        public EmployeeTypeDetailsViewModel()
        {
            model = new EmployeeTypeDetailsModel();
            model.GetEmployeeTypeCollectionCompleted += model_GetEmployeeTypeCollectionCompleted;
            model.GetEmployeeTypeCollectionAsync();
        }

        void model_GetEmployeeTypeCollectionCompleted(object sender, CollectionFeedbackCompletedEventArgs<EmployeeType> e)
        {
            if (e.Error == null)
            {
                if (e.Result != null)
                    EmployeeTypeCollection = e.Result.Data;
                else
                {
                    MessageBox.Show("No result.");
                }
            }
            else 
            {
                MessageBox.Show("Error: " + e.Error);
            }
        }

        private ObservableCollection<EmployeeType> employeeTypeCollection;
        public ObservableCollection<EmployeeType> EmployeeTypeCollection
        {
            get { return employeeTypeCollection; }
            set
            {
                if (!ReferenceEquals(value, employeeTypeCollection))
                {
                    employeeTypeCollection = value;
                    RaisePropertyChanged("EmployeeTypeCollection");
                }
            }
        }
    }
}
